package com.shine.app.adapter;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shine.app.R;
import com.shine.app.bean.CourseImageBean;

public class FlipperGallery implements OnClickListener,
		GestureDetector.OnGestureListener {

	private ViewFlipper viewFlipper;
	private Context context;
	private int count;
	private GalleryAdapter adapter;
	private int length;
	private ImageView leftImageView;
	private ImageView rightImageView;
	private GestureDetector gestureDetector;

	public FlipperGallery() {
	}

	public FlipperGallery(Context context, ViewFlipper vFlipper,
			CourseImageBean[] images, ImageView left, ImageView right,
			ImageLoader loader) {
		this.context = context;
		this.viewFlipper = vFlipper;
		this.leftImageView = left;
		this.rightImageView = right;
		adapter = new GalleryAdapter(context, images, loader);
		count = 0;
		length = images.length;
		viewFlipper.addView(adapter.getView(count, null, null));
		gestureDetector = new GestureDetector(this);
		initOnTouch();
	}

	public FlipperGallery(Context context, ViewFlipper vFlipper,
			CourseImageBean[] images, ImageLoader loader) {
		this.context = context;
		this.viewFlipper = vFlipper;
		adapter = new GalleryAdapter(context, images, loader);
		count = 0;
		length = images.length;
		viewFlipper.addView(adapter.getView(count, null, null));
		gestureDetector = new GestureDetector(this);
	}

	private void initOnTouch() {
		this.leftImageView.setOnClickListener(this);
		this.rightImageView.setOnClickListener(this);
		this.viewFlipper.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return gestureDetector.onTouchEvent(event);
			}
		});
	}

	protected void previous() {
		if (count > 0) {
			count--;
			this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(
					context, R.anim.slide_in_left));
			this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
					context, R.anim.slide_out_right));
			viewFlipper.addView(adapter.getView(count, null, null), 0);
			viewFlipper.showPrevious();
			viewFlipper.removeViewAt(viewFlipper.getChildCount() - 1);
			rightImageView.setVisibility(View.VISIBLE);
			leftImageView.setVisibility(View.VISIBLE);
		} else {
			rightImageView.setVisibility(View.VISIBLE);
			leftImageView.setVisibility(View.GONE);
		}
	}

	protected void next() {
		if (count < length - 1) {
			count++;
			this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(
					context, R.anim.slide_in_right));
			this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
					context, R.anim.slide_out_left));
			viewFlipper.addView(adapter.getView(count, null, null));
			viewFlipper.showNext();
			viewFlipper.removeViewAt(0);
			rightImageView.setVisibility(View.VISIBLE);
			leftImageView.setVisibility(View.VISIBLE);
		} else {
			rightImageView.setVisibility(View.GONE);
			leftImageView.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View view) {
		if (view == leftImageView) {
			previous();
		} else if (view == rightImageView) {
			next();
		} else
			;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e2.getX() - e1.getX() > 120) {
			previous();
			return true;
		} else if (e2.getX() - e1.getX() < -120) {
			next();
			return true;
		}
		return true;

	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
}
